package org.ainlolcat.ainscience.entity.links;

import org.ainlolcat.Variable;
import org.ainlolcat.aincalc2.MissingDependenciesException;
import org.ainlolcat.ainscience.Controller;
import org.ainlolcat.ainscience.column.Column;
import org.ainlolcat.ainscience.entity.Entity;
import org.ainlolcat.ainscience.matrix.MatrixFileParser;

/**
 * @author ainlolcat
 * Date: 11/10/11
 */
public class EqualLink implements Link {
    String name;
    //Integer id;
    Entity entity;
    Integer columnID;
    String l2entity;
    Integer l2column;
    String type;

    //<link name="sensevity" column="0" l2id="201" l2column="0" type="equal"/>
    public EqualLink(String name, Integer columnID, String l2entity, Integer l2column, String type){
        //this.id = id;
        this.columnID = columnID;
        this.name = name;
        this.l2column = l2column;
        this.l2entity = l2entity;
        this.type = type;
    }

    public Integer getId() {
        return entity.getID();
    }

    public Integer getColumnID() {
        return columnID;
    }

    public void setColumnID(Integer columnID) {
        this.columnID = columnID;
    }

    public String getL2Entity() {
        return l2entity;
    }

    public void setL2Entity(String entity) {
        this.l2entity = entity;
    }

    public Integer getL2column() {
        return l2column;
    }

    public void setL2column(Integer l2column) {
        this.l2column = l2column;
    }

    public String getType() {
        return type;
    }

    public void setType(String type) {
        this.type = type;
    }

    public String getName() {
        return name;
    }

    public void setEntity(Entity entity) {
        this.entity = entity;
    }

    public Entity getEntity() {
        return entity;
    }

    public Variable getValue(Integer columnId, Integer rowNum) {
        Entity linked = Controller.getInstance().getProject().getEntityByName(l2entity);
        if (linked == null)
            throw new MissingDependenciesException(l2entity);
        Entity home = this.entity;
        Column c1 = home.getColumnById(this.columnID);
        Column c2 = linked.getColumnById(this.l2column);
        Column c3 = linked.getColumnById(columnId);
        if (type.equals("equal")) {
            if ( c1!=null && c2!=null && c1.getType().equals(c2.getType()) ){
                Integer type = c1.getType();
                Object svalue = c3.getValue(
                        c2.findIndex(c1.getValue(rowNum))
                    );
                if (type.equals(MatrixFileParser.MATRIX_DELIMETER_TYPE_DOUBLE)){
                    return new Variable(svalue,Variable.TYPE_DOUBLE);
                } else if (type.equals(MatrixFileParser.MATRIX_DELIMETER_TYPE_INTEGER)){
                    return new Variable(svalue,Variable.TYPE_INTEGER);
                } else if (type.equals(MatrixFileParser.MATRIX_DELIMETER_TYPE_STRING)){
                    return new Variable(svalue,Variable.TYPE_STRING);
                } else if (type.equals(MatrixFileParser.MATRIX_DELIMETER_TYPE_BOOLEAN)){
                    return new Variable(svalue,Variable.TYPE_BOOLEAN);
                }
            }
        }
        return null;  //To change body of implemented methods use File | Settings | File Templates.
    }

    public boolean isResolved() {
        Entity linked = Controller.getInstance().getProject().getEntityByName(l2entity);
        if (linked==null) return false;

        Entity home = this.entity;
        if (home == null) return false;

        Column c1 = home.getColumnByOrder(this.columnID);
        Column c2 = linked.getColumnByOrder(this.l2column);
        if (c1 == null||c2==null) return false;

        return true;
    }
}
